package com.hn.shopadmin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hn.kiss.modules.sys.shopcommon.common.ParmException;
import com.hn.kiss.modules.sys.shopdb.entity.Role;
import com.hn.kiss.modules.sys.shopdb.entity.RoleExample;
import com.hn.kiss.modules.sys.shopdb.mapper.RoleMapper;
import com.hn.shopadmin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceimpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int insert(String code, String name, String discribe) throws ParmException {
        //校验code和name不重复
        validatorCodeAndName(code, name);

        Role role = new Role();
        role.setCode(code);
        role.setName(name);
        role.setDiscribe(discribe);
        role.setIsdel(0);

        return roleMapper.insert(role);
    }

    @Override
    public Role getByCode(String code) {
        return roleMapper.selectByPrimaryKey(code);
    }

    @Override
    public int update(String code, String name, String discribe) throws ParmException{
        //获取原数据
        Role role1 = roleMapper.selectByPrimaryKey(code);

        //角色名称与原名称不相同,进行校验是否重复
        if (!name.equals(role1.getName())){
            RoleExample roleExample = new RoleExample();
            roleExample.createCriteria().andNameEqualTo(name);
            List<Role> roles = roleMapper.selectByExample(roleExample);
            if (roles.size() > 0){
                throw new ParmException("角色名已存在");
            }
        }

        Role role = new Role();
        role.setCode(code);
        role.setName(name);
        role.setDiscribe(discribe);

        //进行修改
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public List<Role> listALlRole() {
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIsdelEqualTo(0);

        return roleMapper.selectByExample(roleExample);
    }

    @Override
    public PageInfo<Role> listPageRole(Integer page, Integer limit, String keywork) {
        PageHelper.startPage(page, limit);
        if (keywork == null){
            keywork = "%%";
        }else{
            keywork = "%" + keywork.trim() + "%";
        }

        //模糊检索:角色的代码/名称/描述
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andCodeLike(keywork);
        roleExample.or().andNameLike(keywork);
        roleExample.or().andDiscribeLike(keywork);
        roleExample.setOrderByClause("isdel asc");

        List<Role> roles = roleMapper.selectByExample(roleExample);
        return new PageInfo<Role>(roles);
    }

    @Override
    public int stop(String code) {
        Role role = new Role();
        role.setCode(code);
        role.setIsdel(1);
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public int start(String code) {
        Role role = new Role();
        role.setCode(code);
        role.setIsdel(0);
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public void validatorCodeAndName(String code, String name) throws ParmException{
        //校验code是否已存在
        Role role = roleMapper.selectByPrimaryKey(code);
        if(role != null){//code存在
            throw new ParmException("code已存在");
        }
        //校验name是否存在
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andNameEqualTo(name);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        if(roles.size() != 0 ){//name存在
            throw new ParmException("name已存在");
        }

    }
}
